﻿Imports Microsoft.Windows.Controls
Imports System.ComponentModel

Public Class DataGridListView
    Inherits DataGrid

    Public Sub New()
        Me.AutoGenerateColumns = False
        Me.GridLinesVisibility = DataGridGridLinesVisibility.None
        Me.HeadersVisibility = DataGridHeadersVisibility.Column
        Me.SelectionUnit = DataGridSelectionUnit.FullRow
        Me.CanUserResizeRows = False
        'Me.CanUserAddRows = False
        Me.RowHeaderWidth = 0
        Me.VerticalContentAlignment = Windows.VerticalAlignment.Center
    End Sub

    Public Function Serialize() As XElement

        Dim element = New XElement(Me.Name)
        Dim columns = New XElement("Columns")

        For Each c In Me.Columns
            Dim el = <Column>
                         <Width><%= c.Width %></Width>
                         <DisplayIndex><%= c.DisplayIndex %></DisplayIndex>
                         <Visibility><%= c.Visibility %></Visibility>
                         <SortDirection><%= c.SortDirection %></SortDirection>
                     </Column>

            columns.Add(el)
        Next

        element.Add(columns)
        Return element

    End Function

    Public Sub Deserialize(ByVal elements As XElement)

        Dim index As Integer = 0
        Dim displayIndexList = New Dictionary(Of Integer, Integer)

        Dim i = 0
        For Each e In elements...<Column>
            Dim displayIndex = CInt(e.<DisplayIndex>.Value)
            displayIndexList.Add(i, displayIndex)
            i += 1
        Next

        Dim indexes = From ix In displayIndexList Order By ix.Value
        For i = 0 To indexes.Count - 1
            Me.Columns(indexes(i).Key).DisplayIndex = indexes(i).Value
            Me.UpdateLayout()
        Next


        For Each e In elements...<Column>

            ' TODO
            Try

                'Dim displayIndex = CInt(e.<DisplayIndex>.Value)
                'If 0 <= displayIndex AndAlso displayIndex < Me.Columns.Count Then

                '    Me.Columns(index).DisplayIndex = displayIndex
                '    Me.UpdateLayout()
                'End If

                If e.<Width>.Value <> "Auto" AndAlso e.<Width>.Value IsNot Nothing Then
                    Me.Columns(index).Width = CDbl(e.<Width>.Value)
                Else
                    Me.Columns(index).Width = New DataGridLength(0, DataGridLengthUnitType.Auto)
                End If

                If e.<SortDirection>.Value IsNot Nothing Then
                    Dim view = CollectionViewSource.GetDefaultView(Me.ItemsSource)

                    If e.<SortDirection>.Value = "Ascending" Then
                        view.SortDescriptions.Clear()
                        view.SortDescriptions.Add(New SortDescription(Me.Columns(index).SortMemberPath, ListSortDirection.Ascending))
                        Me.Columns(index).SortDirection = ListSortDirection.Ascending
                        view.Refresh()
                    ElseIf e.<SortDirection>.Value = "Descending" Then
                        view.SortDescriptions.Clear()
                        view.SortDescriptions.Add(New SortDescription(Me.Columns(index).SortMemberPath, ListSortDirection.Descending))
                        Me.Columns(index).SortDirection = ListSortDirection.Descending
                        view.Refresh()
                    End If

                End If

            Catch ex As Exception

            End Try



            index += 1
            If index >= Me.Columns.Count Then
                Exit For
            End If
        Next

        Me.Items.Refresh()
    End Sub

End Class
